package com.controller;

import com.model._do.*;
import com.model.dto.baseDTO.ResultDTO;
import com.service.CollectResultService;
import com.service.EquipmentService;
import com.service.ResultService;
import com.service.SignUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/equip")
public class EquipController {

    @Autowired
    private ResultService resultService;

    @Autowired
    private SignUpService signUpService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private CollectResultService collectResultService;

    @RequestMapping(value = "/getMyEquipById", method = RequestMethod.POST)
    public ResultDTO getEquipById(@RequestBody EquipmentDO equip){
        EquipmentDO myequip = equipmentService.getEquipByEquipId(equip.getEquipId());
        return resultService.createSuccess("success", myequip);
    }

    /**
     * 根据userId，获取与我签约的asUserType=DOCTOR的equip
     * @param user userId
     * @return equip:List
     */
    @RequestMapping(value = "/signedWithMeDoctor", method = RequestMethod.POST)
    public ResultDTO getUserSignWithMe(@RequestBody UserDO user){
        return resultService.createSuccess("acquire success",equipmentService.signedWithMeDoctor(user));
    }

    /**
     * 根据equipId，获取时间最近的collect记录
     * @param equipment equipId
     * @return 最近的采集记录
     */
    @RequestMapping(value = "/getEquipByEquipId", method = RequestMethod.POST)
    public ResultDTO getEquipByEquipId(@RequestBody EquipmentDO equipment){
        List<CollectResultDO> collectList = collectResultService.getCollectByEquipId(equipment.getEquipId());
        CollectResultDO latest = null;
        if(collectList.size() > 0){
            latest = collectList.get(0);
        }
        return resultService.createSuccess("success", latest);
    }

    /**
     * 根据userId，获取所有equip，并且将其按照首字母划分
     * @param sign userId
     * @return 经划分的equip
     */
    @RequestMapping("/signedWithMeDevided")
    public ResultDTO getSIgnedEquipWithMeDevided(@RequestBody SignUpDO sign){
        if(StringUtils.isEmpty(sign.getUserId())){
            return resultService.createFailed("request signed userid is empty");
        }
        Object devides = signUpService.getSignedDevided(sign);
        if(devides != null) {
            return resultService.createSuccess("devide success", devides);
        } else {
            return resultService.createFailed("devide failed");
        }
    }

    /**
     * 根据equip更新基础信息
     * @param equipment
     * @return
     */
    @RequestMapping(value = "/doUpdateEquipInfo", method = RequestMethod.POST)
    public ResultDTO doUpdateEquipInfo(@RequestBody EquipmentDO equipment){
        try {
            equipmentService.doUpdateEquipInfo(equipment);
            return resultService.createSuccess("success");
        } catch (Exception e){
            return resultService.createFailed("failed");
        }
    }

    /**
     * 根据userId，获取签约的所有equipment
     * @param sign userId
     * @return equipment:List
     */
    @RequestMapping("/signedWithMe")
    public ResultDTO getSignedEquipWithMe(@RequestBody SignUpDO sign){
        if(StringUtils.isEmpty(sign.getUserId())){
            return resultService.createFailed("request signed userid is empty");
        }
        return resultService.createSuccess("request success",equipmentService.getSignedWithMe(sign));
    }

    /**
     * 根据userId，获取所有未双向确认的equip
     * @param sign userId
     * @return 所有未双向确认的equip
     */
    @RequestMapping("/getSignUnConfirmDoctor")
    public ResultDTO getSignedUnConfirm(@RequestBody SignUpDO sign){
        return resultService.createSuccess("success", equipmentService.getSignUnConfirmDoctor(sign));
    }
}
